@extends('layouts.admin.master')

@section('content')
    <div id="page-wrapper">
        <div class="row">
            <div class="col-lg-12">
                <br/>
            </div>
            <!-- /.col-lg-12 -->
        </div>
        <!-- /.row -->

        <div class="row">
            <div class="col-lg-12">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        文章列表

                        <div class="pull-right">
                            <a href="{{ route('admin.articles.create') }}" class="btn btn-success btn-xs"><i
                                        class="fa fa-plus fa-fw"></i>添加文章</a>
                        </div>
                    </div>
                    <!-- /.panel-heading -->
                    <div class="panel-body">
                        <div class="panel panel-default">
                            <div class="panel-body">
                                <form action="{{ route('admin.articles.index') }}" class="form-inline" method="get">
                                    <div class="form-group">
                                        <label for="inputTitle">标题</label>
                                        <input type="text" class="form-control" id="inputTitle" name="title" value="{{ old('title') }}">
                                    </div>
                                    <div class="form-group">
                                        <label for="selectCategory">分类</label>
                                        <select class="form-control" id="selectCategory" name="category">
                                            <option value="0" {{ old('category') == '0' ? 'selected="selected"':'' }}>=====全部分类=====</option>
                                            @foreach($articleCategories as $v)
                                                <option value="{{ $v[0] }}" {{ old('category') == $v[0] ? 'selected="selected"':'' }}>{{ str_repeat('&nbsp;&nbsp;', $v[2]) . $v[1] }}</option>
                                            @endforeach
                                        </select>
                                    </div>
                                    <button type="submit" class="btn btn-default">查询</button>
                                </form>
                            </div>
                        </div>
                        <div class="table-responsive">
                            <table class="table table-bordered table-hover">
                                <thead>
                                <tr>
                                    <th></th>
                                    <th>标题</th>
                                    <th>分类名称</th>
                                    <th>发布日期</th>
                                    <th>操作</th>
                                </tr>
                                </thead>
                                <tbody>
                                @foreach($articles as $v)
                                    <tr>
                                        <td><input name="articleId" type="checkbox" value="{{ $v->id }}"></td>
                                        <td>{{ $v->title }}</td>
                                        <td>{{ empty($v->articleCategory->name) ? '':$v->articleCategory->name }}</td>
                                        <td>{{ $v->created_at }}</td>
                                        <td>
                                            <a class="btn btn-info btn-xs"
                                               href="{{ route('admin.articles.edit', ['id' => $v->id]) }}">编辑</a>

                                            <form action="{{ route('admin.articles.destroy', ['id' => $v->id]) }}"
                                                  method="post" style="display: inline;"
                                                  onsubmit="return confirm('确认删除吗？');">
                                                {{ csrf_field() }}
                                                {{ method_field('DELETE') }}
                                                <button class="btn btn-danger btn-xs" type="submit">删除</button>
                                            </form>
                                        </td>
                                    </tr>
                                @endforeach
                                </tbody>
                            </table>
                        </div>
                        <!-- /.table-responsive -->
                        <div class="row">
                            <div class="col-sm-4">
                                <div class="form-group">
                                    <div class="checkbox">
                                        <label>
                                            <input id="chkall" type="checkbox">
                                            全选/全不选
                                        </label>

                                        <form id="formDeleteArticles" action="{{ route('admin.articles.destroys') }}"
                                              method="post" style="display: inline;">
                                            {{ csrf_field() }}
                                            <input type="hidden" id="ids" name="ids">
                                            <button type="button" class="btn btn-danger btn-xs" id="delete-articles">
                                                删除
                                            </button>
                                        </form>
                                    </div>
                                </div>
                            </div>
                            <div class="col-sm-8">
                                {!! $articles->appends(['title' => old('title'), 'category' => old('category')])->render(new \App\Libs\Pagination\AdminPresenter($articles)) !!}
                            </div>
                        </div>
                    </div>
                    <!-- /.panel-body -->
                </div>
                <!-- /.panel -->
            </div>
            <!-- /.col-lg-12 -->
        </div>
        <!-- /.row -->
        <script type="text/javascript">
            $("#chkall").click(function () {
                $('input[name="articleId"]').prop('checked', function (i, val) {
                    return !val;
                });
            });
            $("#delete-articles").click(function () {
                var chk_value = [];
                $('input[name="articleId"]:checked').each(function () {
                    chk_value.push($(this).val());
                });
                if (chk_value.length == 0) {
                    alert('你还没有选择任何内容!');
                } else {
                    $('#ids').val(chk_value.join(','));
                    $('#formDeleteArticles').submit();
                }
            });
        </script>
    </div>
@endsection